import SqliteApi from "./SqliteApi";
const _SqliteModule = require('react-native').NativeModules.SqliteModule;
/**
 * 注意不支持 long 类型,只有 int double
 */
export default class BaseSqlite extends SqliteApi{

    open() {
        _SqliteModule.open(this._schemas(),
            (oldVersion,newVersion)=>{
                this.migration(oldVersion,newVersion);
            });
        return this;
    }
    close(){
        _SqliteModule.close(this.constructor.name);
    }
    /**
     *
     */
    exec(sqlStr):Promise{
        return _SqliteModule.exec(this.constructor.name,sqlStr);
    }

    /**
     * @returns {Promise} 返回 插入的id
     */
    insert(where):Promise{
        return _SqliteModule.insert(this.constructor.name,this._objMap(),where);
    }
    /**
     * @returns {Promise} 返回 删除的条数
     */
    delete(where):Promise{
        return _SqliteModule.delete(this.constructor.name,where);
    }
    /**
     * @returns {Promise} 返回 查询的list 集合
     */
    select(where):Promise{
        return _SqliteModule.select(this.constructor.name,where);
    }
    /**
     * @returns {Promise} 返回 更新的条数
     */
    update(where):Promise{
        return _SqliteModule.update(this.constructor.name,this._objMap(),where);
    }
    _objMap(){
        let ret = {};
        for (let key in this) {
            if (this[key] instanceof Date){
                ret[key] = this[key].getTime();
            }else if (this[key] instanceof Function){
            }else{
                ret[key] = this[key];
            }
        }
        return ret;
    }

}
